export const KEY_SELET = function (_, generator) {
    var code = this.getFieldValue('KEY');
    return [code, generator.ORDER_ATOMIC];
}

export const AS_KEY_SELET = function (_, generator) {
    var code = this.getFieldValue('KEY');
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_light_init = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    var code = 'adc=board.adc_init(0x48,0)\n';
    return code;
}

export const sensor_light_read = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    var code = 'board.adc_read(adc,3)';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_button_init = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    var key = generator.valueToCode(this, 'key', generator.ORDER_ATOMIC);
    var code1 = 'button' + key + '=board.pin(' + key + ',board.GPIO.IN,board.GPIO.PULL_UP)\n';
    return code1;
}

export const sensor_button_read = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    var key = generator.valueToCode(this, 'key', generator.ORDER_ATOMIC);
    var code = 'not button' + key + '.value()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_adc_init = function (_, generator) {
    generator.definitions_['import ads1x15'] = 'import ads1x15';
    var mode = generator.valueToCode(this, 'key', generator.ORDER_ATOMIC);
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = 'adc=ads1x15.ADS1015(' + sub + ',address=0x48,gain=' + mode + ')\n';
    return code;
}

export const sensor_adc_read = function (_, generator) {
    generator.definitions_['import ads1x15'] = 'import ads1x15';
    var pin = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var code = "";
    code = 'adc.read(channel1=' + pin + ')';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_adc_Vread = function (_, generator) {
    generator.definitions_['import ads1x15'] = 'import ads1x15';
    var pin = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var code = "";
    code = 'adc.raw_to_v(adc.read(channel1=' + pin + '))';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_dht11 = function (_, generator) {
    generator.definitions_['import dht11_no'] = 'import dht11_no';
    var TYPE = this.getFieldValue('TYPE');
    var PIN = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var code = '';
    if (TYPE == "2")
        code = 'dht11_no.read_data(' + PIN + ')';
    else
        code = 'dht11_no.read_data(' + PIN + ')[' + TYPE + ']';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_mpu9250_attachGestureInterrupt = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    var gesture = this.getFieldValue('gesture');
    var branch = generator.statementToCode(this, 'DO');
    var d = branch || generator.PASS;
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "mpu")
        generator.definitions_['import_mixgo_mpu'] = 'from mixgo import mpu';
    var code = 'if ' + v + '.mpu9250_is_gesture("' + gesture + '"):\n' + d;
    return code;
}

export const sensor_mpu9250_gesture = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    var gesture = this.getFieldValue('gesture');
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "mpu")
        generator.definitions_['import_mixgo_mpu'] = 'from mixgo import mpu';
    var code = v + '.mpu9250_is_gesture("' + gesture + '")';
    return [code, generator.ORDER_ATOMIC];
}

//ok
export const sensor_mpu9250_get_acceleration = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    var key = this.getFieldValue('key');
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "mpu")
        generator.definitions_['import_mixgo_mpu'] = 'from mixgo import mpu';
    var code = v + '.mpu9250_get_' + key + '()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_adxl345_get_acceleration = function (_, generator) {
    generator.definitions_['import_adxl345'] = 'import adxl345_no';
    var key = this.getFieldValue('key');
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code;
    if (key == 'x') {
        code = v + '.readX()';
    } else if (key == 'y') {
        code = v + '.readY()';
    } else if (key == 'z') {
        code = v + '.readZ()';
    } else if (key == 'values') {
        code = v + '.readXYZ()';
    }
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_mpu9250_get_magnetic = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    var key = this.getFieldValue('key');
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "mpu")
        generator.definitions_['import_mixgo_mpu'] = 'from mixgo import mpu';
    var code = v + '.mpu9250_magnetic_' + key + '()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_mpu9250_get_gyro = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    var key = this.getFieldValue('key');
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "mpu")
        generator.definitions_['import_mixgo_mpu'] = 'from mixgo import mpu';
    var code = v + '.mpu9250_gyro_' + key + '()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_mpu9250_calibrate_compass = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    generator.definitions_['import_mixgo_compass'] = 'from mixgo import compass';
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    return '' + v + '.calibrate()\n';
}

export const sensor_mpu9250_temperature = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "mpu")
        generator.definitions_['import_mixgo_mpu'] = 'from mixgo import mpu';
    return [v + '.mpu9250_get_temperature()', generator.ORDER_ATOMIC];
}

export const sensor_mpu9250_field_strength = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    generator.definitions_['import_machine'] = 'import machine';
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    if (v == "compass")
        generator.definitions_['import_mixgo_compass'] = 'from mixgo import compass';
    var compass = this.getFieldValue('compass');
    var a;
    if (compass == 'strength') {
        a = v + '.get_field_strength()';
    }
    else if (compass == 'heading') {
        a = v + '.heading()';
    }
    return [a, generator.ORDER_ATOMIC];
}

export const sensor_compass_reset = function (_, generator) {
    generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
    generator.definitions_['import_mixgo_compass'] = 'from mixgo import compass';
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    return '' + v + '.reset_calibrate()\n';
}

export const HCSR04 = function (_, generator) {
    generator.definitions_['import_sonar'] = 'import sonar_no';
    var dropdown_pin1 = generator.valueToCode(this, "PIN1", generator.ORDER_ASSIGNMENT);
    var dropdown_pin2 = generator.valueToCode(this, "PIN2", generator.ORDER_ASSIGNMENT);
    var code = 'sonar_no.Sonar(' + dropdown_pin1 + ', ' + dropdown_pin2 + ')';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_use_i2c_init = function (_, generator) {
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var iv = generator.valueToCode(this, 'I2CSUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('key');
    var code;
    if (key == 'MPU9250') {
        generator.definitions_['import_mpu9250'] = 'import mpu9250_no';
        code = v + ' = mpu9250_no.' + key + "(" + iv + ')\n';
    } else if (key == 'BMP280') {
        generator.definitions_['import_bmp280'] = 'import bmp280_no';
        code = v + ' = bmp280_no.' + key + "(" + iv + ')\n';
    } else if (key == 'SHT20') {
        generator.definitions_['import_sht20'] = 'import sht20_no';
        code = v + ' = sht20_no.' + key + "(" + iv + ')\n';
    } else if (key == 'ADXL345') {
        generator.definitions_['import_adxl345'] = 'import adxl345_no';
        code = v + ' = adxl345_no.' + key + "(" + iv + ')\n';
    }
    return code;
}

export const sensor_bmp = function (_, generator) {
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('key');
    generator.definitions_['import_bmp280'] = 'import bmp280_no';
    var code = v + '.' + key;
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_sht = function (_, generator) {
    var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('key');
    generator.definitions_['import_sht20'] = 'import sht20_no';
    var code = v + '.' + key;
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_ds18x20 = function (_, generator) {
    generator.definitions_['import_ds18x20x'] = 'import ds18x20x';
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var code = 'ds18x20x.get_ds18x20_temperature(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_lm35 = function (_, generator) {
    generator.definitions_['import_lm35'] = 'import lm35';
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var code = 'lm35.get_LM35_temperature(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
}

//--开始--------------------------新增-20211227-------------------------------------------------
export const sensor_asr_init = function (_, generator) {
    generator.definitions_['import asr3320'] = 'import asr3320';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var i2c = generator.valueToCode(this, 'SUBI2C', generator.ORDER_ATOMIC);
    var code = sub + " = asr3320.ASR3320(" + i2c + ")\n";
    return code;
}

export const sensor_asr_eraseWords = function (_, generator) {
    generator.definitions_['import asr3320'] = 'import asr3320';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = sub + ".eraseWords()\n";
    return code;
}

export const sensor_asr_addWords = function (_, generator) {
    generator.definitions_['import asr3320'] = 'import asr3320';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var idNum = generator.valueToCode(this, 'idNum', generator.ORDER_ATOMIC);
    var words = generator.valueToCode(this, 'words', generator.ORDER_ATOMIC);
    var code = sub + ".addWords(" + idNum + "," + words + ")\n";
    return code;
}

export const sensor_asr_esetMode = function (_, generator) {
    generator.definitions_['import asr3320'] = 'import asr3320';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('TYPE');
    var code = sub + ".setMode(" + key + ")\n";
    return code;
}

export const sensor_asr_getResult = function (_, generator) {
    generator.definitions_['import asr3320'] = 'import asr3320';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = sub + ".getResult()";
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_apds9960_init = function (_, generator) {
    generator.definitions_['import apds9960'] = 'import apds9960';
    var key = this.getFieldValue('TYPE');
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var i2c = generator.valueToCode(this, 'SUBI2C', generator.ORDER_ATOMIC);
    var code1 = sub + "= apds9960.APDS9960(" + i2c + ")\n";
    var code2;
    if (key == '1') {
        code2 = sub + '.enableGestureSensor()\n';
    } else if (key == '2') {
        code2 = sub + '.enableLightSensor()\n';
    } else if (key == '3') {
        code2 = sub + '.enableProximitySensor()\n';
    }
    return code1 + code2;
}

export const sensor_apds9960_readGesture = function (_, generator) {
    generator.definitions_['import apds9960'] = 'import apds9960';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('TYPE');
    var code;
    if (key == '1') {
        code = sub + '.readGesture()';
    } else if (key == '2') {
        code = sub + '.readProximity()';
    }
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_apds9960_readLRGB = function (_, generator) {
    generator.definitions_['import apds9960'] = 'import apds9960';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('TYPE');
    var code;
    if (key == '1') {
        code = sub + '.readRedLight()';
    } else if (key == '2') {
        code = sub + '.readGreenLight()';
    } else if (key == '3') {
        code = sub + '.readBlueLight()';
    } else if (key == '4') {
        code = sub + '.readAmbientLight()';
    }
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_as608_init = function (_, generator) {
    generator.definitions_['import as608'] = 'import as608';
    var key = this.getFieldValue('mode');
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = sub + "= as608.AS608(uart" + key + ")\n";
    return code;
}

export const sensor_as608_savefig = function (_, generator) {
    generator.definitions_['import as608'] = 'import as608';
    var idNum = generator.valueToCode(this, 'idNum', generator.ORDER_ATOMIC);
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = sub + ".savefig(" + idNum + ")\n";
    return code;
}

export const sensor_as608_disfig = function (_, generator) {
    generator.definitions_['import as608'] = 'import as608';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = sub + ".disfig()";
    return [code, generator.ORDER_ATOMIC];
}

//--结束---------------------------新增-20211227-------------------------------------------------


//--开始--------------------------Amigo硬件 -20210920-------------------------------------------------

export const Amigo_KEY_SELET = function (_, generator) {
    var code = this.getFieldValue('KEY');
    return [code, generator.ORDER_ATOMIC];
}

//--开始-------------编码测速---20230309---------------------
export const sensor_encoder_init = function (_, generator) {
    generator.definitions_['import encoder'] = 'import encoder';
    var SUB = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var scale = generator.valueToCode(this, 'scale', generator.ORDER_ATOMIC);
    var wheel = generator.valueToCode(this, 'wheel', generator.ORDER_ATOMIC);
    var code = '' + SUB + '=encoder.ENCODER(' + key + ',' + scale + ',' + wheel + ')\n';
    return code;
}

export const sensor_encoder_data = function (_, generator) {
    generator.definitions_['import encoder'] = 'import encoder';
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var key = this.getFieldValue('TYPE');
    var code = sub + '.' + key + '';
    return [code, generator.ORDER_ATOMIC];
}

//---------------TABIT板载传感器资源---20230520--------------------
export const sensor_tabit_charged = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_tabit_button = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var key = this.getFieldValue('TYPE');
    var code = 'ob_key.pressed(' + key + ')';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_tabit_acc = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var key = this.getFieldValue('TYPE');
    var code = 'ob_acc.' + key + '';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_tabit_acc_euler = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var key = this.getFieldValue('TYPE');
    var code = 'ob_acc.' + key + '';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_tabit_als = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var key = this.getFieldValue('TYPE');
    var code = 'ob_als.' + key + '';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_tabit_ps = function (_, generator) {
    generator.definitions_['from tabit import *'] = 'from tabit import *';
    var code = 'ob_als.ps_nl()';
    return [code, generator.ORDER_ATOMIC];
}

//---------------ASBIT板载传感器资源---20230722-------------------
export const sensor_asbit_button = function (_, generator) {
    generator.definitions_['from asbit import *'] = 'from asbit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.is_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

//---------------LTBIT板载传感器资源---20230722-------------------
export const actuator_ltbit_set_HALL = function (_, generator) {
    generator.definitions_['import ltbit'] = 'import ltbit';
    generator.definitions_['import math'] = 'import math';
    var scale = generator.valueToCode(this, 'scale', generator.ORDER_ATOMIC);
    var wheel = generator.valueToCode(this, 'wheel', generator.ORDER_ATOMIC);
    var code1 = 'ltbit.pulse_turns_1 = 1 / ' + scale + '\nltbit.pulse_distance_1 = math.pi * ' + wheel + ' / ' + scale + '\n';
    var code2 = 'ltbit.pulse_turns_2 = 1 / ' + scale + '\nltbit.pulse_distance_2 = math.pi * ' + wheel + ' / ' + scale + '\n';
    var code3 = 'ltbit.pulse_turns_3 = 1 / ' + scale + '\nltbit.pulse_distance_3 = math.pi * ' + wheel + ' / ' + scale + '\n';
    var code4 = 'ltbit.pulse_turns_4 = 1 / ' + scale + '\nltbit.pulse_distance_4 = math.pi * ' + wheel + ' / ' + scale + '\n';
    return code1 + code2 + code3 + code4;
}

export const sensor_ltbit_hall_data = function (_, generator) {
    generator.definitions_['import ltbit'] = 'import ltbit';
    var mode = this.getFieldValue('mode');
    var data = this.getFieldValue('data');
    var code = '';
    if (data == "0")
        code = 'ltbit.turns_' + mode + '';
    if (data == "1")
        code = 'ltbit.distance_' + mode + '';
    if (data == "2")
        code = 'ltbit.speed_' + mode + '()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_ltbit_hall_setdata = function (_, generator) {
    generator.definitions_['import ltbit'] = 'import ltbit';
    var mode = this.getFieldValue('mode');
    var data = this.getFieldValue('data');
    var value = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var code = '';
    if (data == "0")
        code = 'ltbit.turns_' + mode + ' = ' + value + '\n';
    if (data == "1")
        code = 'ltbit.distance_' + mode + ' = ' + value + '\n';
    return code;
}

export const sensor_ltbit_hall_irq = function (_, generator) {
    generator.definitions_['import ltbit'] = 'import ltbit';
    var mode = this.getFieldValue('mode');
    var atta = generator.valueToCode(this, 'DO', generator.ORDER_ATOMIC);
    var code = 'ltbit.irq_cb_' + mode + '(' + atta + ')\n';
    return code;
}

export const sensor_ltbit_button = function (_, generator) {
    generator.definitions_['from ltbit import *'] = 'from ltbit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.is_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

//---------------AIBIT板载传感器资源---20240122-------------------
export const sensor_aibit_button = function (_, generator) {
    generator.definitions_['from aibit import *'] = 'from aibit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.is_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_aibit_button1 = function (_, generator) {
    generator.definitions_['from aibit import *'] = 'from aibit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.was_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_aibit_brightness = function (_, generator) {
    generator.definitions_['from aibit import *'] = 'from aibit import *';
    var code = 'brightness()';
    return [code, generator.ORDER_ATOMIC];
}

//-------------RTC---------------------
export const sensor_rtc_set = function (_, generator) {
    generator.definitions_['import time'] = 'import time';
    var LIST = generator.valueToCode(this, 'LIST', generator.ORDER_ATOMIC);
    var code = "time.set_time(" + LIST + ")\n";
    return code;
}

export const sensor_rtc_get = function (_, generator) {
    generator.definitions_['import time'] = 'import time';
    var code = 'time.localtime(time.time())[:-1]';
    return [code, generator.ORDER_ATOMIC];
}